package com.zte.market.control;

import com.zte.login.pojo.Users;
import com.zte.market.pojo.Order;
import com.zte.market.pojo.Orderdetails;
import com.zte.market.pojo.otherPojo.CBrand;
import com.zte.market.pojo.otherPojo.CProduct;
import com.zte.market.pojo.otherPojo.CType;
import com.zte.market.pojo.otherPojo.DateTemp;
import com.zte.market.service.CustomService;
import com.zte.market.service.OrderService;
import com.zte.market.service.OrderdetailsService;
import com.zte.market.service.otherService.CBrandService;
import com.zte.market.service.otherService.CProductService;
import com.zte.market.service.otherService.CTypeService;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: 许月
 * @Date: 2022/10/05/19:19
 */
@Controller
@RequestMapping("/orderExamine")
@SessionAttributes("orderPage")
public class OrderExamineController {
    @Autowired
    private OrderService orderService; //订单 service

    @Autowired
    private OrderdetailsService orderdetailsService;  //订单详情 service

    @Autowired
    private CBrandService cBrandService; //品牌 service

    @Autowired
    private CTypeService cTypeService;  //类型 service

    @Autowired
    private CProductService cProductService; //商品 service

    @Autowired
    private CustomService customService;

    /**
     * 分页查询
     *
     * @param orderid    订单号
     * @param customname 客户名
     * @param startDate  最早时间
     * @param endDate    最晚时间
     * @param minPrice   最低金额
     * @param maxPrice   最高金额
     * @param pageNum    当前页
     * @param pageSize   页面尺寸
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("/queryfenye")
    public String queryfenye(String orderid, String customname, @RequestParam(value = "startDate", defaultValue = "")String startDate,
                             @RequestParam(value = "endDate", defaultValue = "")String endDate, Double minPrice,
                             Double maxPrice, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize, Model model, HttpSession session) {
        //获取登录用户信息
        Users user = (Users) session.getAttribute("user");

        //将前端传来的时间字符串转换格式
        //开始时间
        String startDate1 = null;
        if (startDate != null && !"".equals(startDate)) {

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date d = null;
            try {
                d = format.parse(startDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            DateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            startDate1 = dateformat.format(d);

        }
        //结束时间
        String endDate1 = null;
        if (endDate != null && !"".equals(endDate)) {

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date d = null;
            try {
                d = format.parse(endDate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            DateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            endDate1 = dateformat.format(d);
        }

        //临时的时间对象（自定义的，存储String类型）
        DateTemp dateTemp = new DateTemp();
        dateTemp.setStartDate(startDate1);
        dateTemp.setEndDate(endDate1);
        //通过用户名获取 用户所对应的id列表（模糊查询）
        List<Integer> customIdList = customService.queryCustomIdListByCustomname(customname);
        //由于是审核页面
        String status = "审核中";
        //进行订单的查询操作
        List<Order> orderList = orderService.getAllByParams(orderid, customIdList,dateTemp, minPrice, maxPrice, status);
        //根据登录用户获取对应的数据（普通员工没有审核资格，）
        List<Order> orderListTemp = new ArrayList<>();
        if (user.getDeptId() == 1) {
//            市场部
            for (Order order : orderList) {
                if (order.getCheckid() == user.getUid()) {
                    orderListTemp.add(order);
                }
            }
        } else {
            orderListTemp = orderList;
        }
        //将数据封装在分页对象中
        PageModel<Order> orderPage = orderService.queryByPage(orderListTemp, pageSize, pageNum);
        model.addAttribute("orderPage", orderPage);
//        进行数据回显操作
        model.addAttribute("orderid", orderid);  //订单编号输入框
        model.addAttribute("customname", customname); //客户姓名输入框
        model.addAttribute("startDate", startDate);//开始时间输入框
        model.addAttribute("endDate", endDate);   //最终时间输入框
        model.addAttribute("minPrice", minPrice);  //最低金额输入框
        model.addAttribute("maxPrice", maxPrice);   //最高金额输入框

        return "market/orderExamine/orderExamineList";
    }



    /**
     * 去审核页面
     * @param orderid
     * @return
     */
    @RequestMapping("/tocheck")
    public String tocheck (String orderid,Model model){

        Order order = orderService.getByOrderId(orderid);
        model.addAttribute("order",order);
        return "market/orderExamine/orderExamine";
    }

    /**
     * 对订单进行修改（对应前端的审核操作）
     * @param orderId 订单编号
     * @param opinion 评价
     * @param status 审核状态
     * @param chectime 审核时间
     * @param session
     * @return
     */
    @RequestMapping("/updateOrder")
    public String updateOrder(String orderId,String opinion,String status,String chectime ,HttpSession session){
        System.out.println("******************************************************");
        System.out.println(orderId);

        //进行修改时间的格式化操作
        Date  chectime1 = new Date(System.currentTimeMillis());
        if(chectime != null && !"".equals(chectime)){

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd Hh:mm:ss");
            java.util.Date d = null;
            try {
                d = format.parse(chectime);
            } catch (Exception e) {
                e.printStackTrace();
            }
            chectime1 = new java.sql.Date(d.getTime());
        }
//        将修改需要的信息进行封装
        Order order = new Order();
        order.setChectime(chectime1);
        order.setOrderid(orderId);
        order.setOpinion(opinion);
        order.setStatus(status);

        //修改订单操作
        Integer result1 = orderService.updateByOrder(order);


        PageModel<Order> orderPage = (PageModel<Order>) session.getAttribute("orderPage");
        int pageNum = orderPage.getPageNum();
        return "redirect:/orderExamine/queryfenye?pageNum=" + pageNum;
    }


}
